package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.bankInterface.IReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.text.StringHelper;
import org.springframework.stereotype.Component;

@Component("CMBC01Receipt")
public class CMBC01Receipt extends CMBC01Base implements IReceipt {

    @Override
    public RetAtsReceipt qryReceipt(ReqQryReceipt p_reqReceipt) throws Exception {
        //电子回单查询返回ATS的对象
        synchronized (CMBC01Receipt.class) {
            RetAtsReceipt retAts = new RetAtsReceipt(p_reqReceipt.reqHeader);
            String receiptSaveToMainDir = FGBiz.dspCenter.dspSet.receiptMainDir;
            if (!StringHelper.isNullOrEmpty(receiptSaveToMainDir)) {
                if (!StringHelper.isNullOrEmpty(FGBiz.dspCenter.dspSet.receiptShareDir)) {
                    if (!StringHelper.isNullOrEmpty(FGBiz.dspCenter.dspSet.receiptPushServerUrl)) {
                        CMBC01SyncTask tmpTask = new CMBC01SyncTask(p_reqReceipt.actNum, p_reqReceipt.dateFrom, p_reqReceipt.dateTo);
                        tmpTask.bankFront = this.bankFront;
                        tmpTask.reqHeader = this.reqHeader;
                        tmpTask.downloadReceipt();
                        if (getBankConfig().getValueAsString("waitSecsEachReqForReceipt").length() > 0) {
                            long sleepTime = Long.parseLong(getBankConfig().getValueAsString("waitSecsEachReqForReceipt")) * 1000;
                            Thread.sleep(sleepTime);
                        } else {
                            Thread.sleep(1000L);
                        }
                        retAts.respCode = "0";
                        retAts.respInfo = "后台处理中";
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = "DSPSystem未配置回单文件推送地址";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "DSPSystem未配置回单文件共享目录";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "DSPSystem未配置回单文件下载保存主目录";
            }
            retAts.writeRetToATS();
            return retAts;
        }
    }
}
